package com.lzh.template.controller;

import com.lzh.template.annotation.LogAnnotation;
import com.lzh.template.common.BaseResponse;
import com.lzh.template.common.LogType;
import com.lzh.template.common.ResultUtil;
import com.lzh.template.model.dto.user.LoginUserDto;
import com.lzh.template.model.dto.user.UserRegisterDto;
import com.lzh.template.model.vo.user.LoginUserVo;
import com.lzh.template.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author luzhiheng
 * @description 用户控制器
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @LogAnnotation(message = "用户登录", operation = LogType.LOGIN)
    @PostMapping("/login")
    public BaseResponse<Map<String, Object>> login(@RequestBody LoginUserDto dto) {
        return userService.login(dto);
    }

    @LogAnnotation(message = "用户注册", operation = LogType.ADD)
    @PostMapping("/regist")
    public BaseResponse regist(@RequestBody UserRegisterDto dto) {
        return userService.regist(dto);
    }

    @LogAnnotation(message = "退出登录", operation = LogType.LOGOUT)
    @PostMapping("/logout")
    public BaseResponse logout() {
        return userService.logout();
    }

    /**
     * 获取当前登录用户
     *
     * @param request
     * @return
     */
    @GetMapping("/get/login")
    public BaseResponse<LoginUserVo> getLoginUser(HttpServletRequest request) {
        LoginUserVo user = userService.getLoginUser(request);
        return ResultUtil.success(user);
    }

    @LogAnnotation(message = "更新用户头像", operation = LogType.UPDATE)
    @PostMapping("/avatar")
    public BaseResponse updateUserAvatar(Long userId, String avatar) {
        return userService.updateUserAvatar(userId, avatar);
    }

//    @LogAnnotation(message = "更新用户信息", operation = LogType.UPDATE)
//    @PutMapping("/updateUser")
//    public ResponseResult updateUser(@RequestBody UpdateUserDTO updateUserDTO) {
//        User user = BeanCopyUtils.copyBean(updateUserDTO, User.class);
//        userService.updateById(user);
//        return ResponseResult.success();
//    }

//    @LogAnnotation(message = "更新密码", operation = LogType.UPDATE)
//    @PutMapping("/updatePassword")
//    public ResponseResult updatePassword(@RequestBody UpdatePwdDTO updatePwdDTO) {
//        return userService.updatePasswordByUserId(updatePwdDTO);
//    }

//    @LogAnnotation(message = "重置密码", operation = LogType.UPDATE)
//    @PutMapping("/rePassword")
//    public ResponseResult rePassword(@RequestBody UpdatePwdDTO user) {
//        return userService.rePassword(user.getEmail(), user.getNewPassword(), user.getConPassword());
//    }

    @LogAnnotation(message = "注销账号", operation = LogType.DELETE)
    @DeleteMapping("/cancel/{userId}")
    public BaseResponse<Object> cancelAccount(@PathVariable Long userId) {
        return userService.cancelAccount(userId);
    }

}
